package com.encryption.rpc.facade
{
	import com.encryption.rpc.RPCEvent;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	public class DepartmentFacade
	{
		public static const OPERATION_GETALL:String = "getAll";
		public static const OPERATION_ADD_DEPART:String = "addDepart";
		public static const OPERATION_EDIT_DEPART:String = "editDepart";
		public static const OPERATION_DELETE_DEPART:String = "deleteDepart";
		
		private var remoteObject:RemoteObject;
		private var handler:Function;
		private var argsObj:Object;
		
		public function DepartmentFacade(operation:String,argsObj:Object,handler:Function){
			remoteObject = new RemoteObject();
			remoteObject.destination = "departmentFacade";
			this.handler = handler;
			this.argsObj = argsObj;
			
			if(operation == OPERATION_GETALL){
				getAll();
			}else if(operation == OPERATION_ADD_DEPART){
				addDepart();
			}else if(operation == OPERATION_EDIT_DEPART){
				editDepart();
			}else if(operation == OPERATION_DELETE_DEPART){
				deleteDepart();
			}
		}
		
		private function getAll():void{
			remoteObject.getAll();
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function addDepart():void{
			remoteObject.addDepart(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function editDepart():void{
			remoteObject.editDepart(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function deleteDepart():void{
			remoteObject.deleteDepart(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function onFault(evt:FaultEvent):void{
			var rpcEvent:RPCEvent = new RPCEvent(evt);
			this.handler(rpcEvent);
		}
		
		private function onResult(evt:ResultEvent):void{
			remoteObject.disconnect();
			var rpcEvent:RPCEvent = new RPCEvent(evt);
			this.handler(rpcEvent);
		}
	}
}